package com.one.shopping.ui.fragment;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.one.shopping.R;
import com.one.shopping.adapter.CommonAdapter;
import com.one.shopping.adapter.UsersAdapter;
import com.one.shopping.api.OneApi;
import com.one.shopping.entity.Result;
import com.one.shopping.entity.Users;
import com.one.shopping.ui.activity.UserDetailActivity;
import com.one.shopping.ui.baseFragment.BaseSwipeRefreshFragment;
import com.one.shopping.util.GsonUtils;

import org.apache.http.Header;

import java.util.List;

/**
 * @author 柯尊诚
 * @version 1.0
 * @date 创建时间：2016/1/10 9:59
 */
public class UsersItemFragment extends BaseSwipeRefreshFragment<Users> {

    @Override
    public CommonAdapter<Users> getAdapter() {
        return new UsersAdapter(getActivity(), R.layout.list_item_user);
    }

    @Override
    public List<Users> getDatas(byte[] responeString) {
        Result result = GsonUtils.fromJson(new String(responeString), Result.class);
        List<Users> userList = GsonUtils.json2List(result.getData(), Users.class);
        return userList;
    }

    @Override
    public void requestData() {
        OneApi.getUserList(mHandler);
    }

    @Override
    public void onItemClick(int position, Users users) {
        Toast.makeText(getActivity(), users.getName(), Toast.LENGTH_SHORT).show();

        Intent intent = new Intent(getActivity(), UserDetailActivity.class);
        Bundle bundle = new Bundle();
        bundle.putSerializable("users", users);
        intent.putExtras(bundle);
        startActivity(intent);
    }

    @Override
    public void onItemLongClick(int position, final Users users) {
        // 弹出对话框删除
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        builder.setMessage("是否删除？");
        builder.setTitle("提示");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int arg1) {

                delete(users.getId(), dialog);

            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int arg1) {
                dialog.dismiss();

            }
        });
        builder.create().show();
    }

    private void delete(int id, final DialogInterface dialog) {
        OneApi.deleteUser(id, new AsyncHttpResponseHandler() {
            @Override
            public void onSuccess(int i, Header[] headers, byte[] bytes) {
                Result result = GsonUtils.fromJson(new String(bytes), Result.class);
                if (result.getCode() == 200) {
                    onRefresh();
                }
                Toast.makeText(getActivity(), result.getMessage(), Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                Toast.makeText(getActivity(), "网络错误！", Toast.LENGTH_SHORT).show();
                dialog.dismiss();
            }
        });
    }

    @Override
    public void onResume() {
        super.onResume();

        onRefresh();
    }
}
